package com.seed.controller;

import com.seed.entity.User;
import com.seed.qo.UserQo;
import com.seed.service.UserService;
import com.seed.vo.Result;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    private Log log = LogFactory.getLog(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/get")
    public Result<User> get(@RequestParam("id") Integer id) {
        log.info("id=" + id);
        return userService.get(id);
    }

    @GetMapping(value = "/save")
    public Result<Integer> save() {
        User user = new User();
        user.setName("peter");
        return userService.save(user);
    }

    @GetMapping(value = "/query")
    public Result<User> query(UserQo userQo) {
        return userService.query(userQo);
    }

    @GetMapping(value = "/get2")
    public String get2(@RequestParam("id") Integer id) {
        log.info("id=" + id);
        String str = restTemplate.getForObject("http://seed-org/user/get?id={id}", String.class, id);
        return str;
    }

    @GetMapping(value = "/get3")
    public String get3(@RequestParam("id") Integer id) {
        log.info("id=" + id);
        return "id:" + id;
    }
}
